/*
 * Copyright 2022 The Chromium Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 *
 * The default style sheet used to render <selectmenu> elements
 * (HTMLSelectMenuElement enabled).
 */

@namespace "http://www.w3.org/1999/xhtml";

selectmenu {
  display: inline-block;
  user-select: none;
  font-family: sans-serif;
  font-size: .875em;
}

selectmenu::-internal-selectmenu-button {
  display: inline-flex;
  align-items: center;
  cursor: default;
  appearance: none;
  background-color: Field;
  color: ButtonText;
  border: 1px solid ButtonBorder;
  border-radius: 0.25em;
  padding: 0.25em;
}

/* TODO(masonf) We need an internal pseudo for the selected value part */
selectmenu::part(selected-value) {
  color: HighlightText;
}

selectmenu option {
  font-size: .875em;
  padding: 0.25em;
}

selectmenu option:hover {
  cursor: default;
  user-select: none;
  background-color: SelectedItem;
  color: SelectedItemText;
}

selectmenu optgroup {
  padding-bottom: 0.75em;
}

selectmenu optgroup:last-child {
  padding-bottom: 0;
}

selectmenu optgroup > option {
  padding-left: 0.75em;
}

selectmenu optgroup > option {
  padding-left: 0.75em;
}

selectmenu::-internal-selectmenu-button-icon {
  background-image: -internal-light-dark(url(images/selectmenu_button_icon.svg), url(images/selectmenu_button_icon_white.svg));
  background-origin: content-box;
  background-repeat: no-repeat;
  background-size: contain;
  height: 1.0em;
  margin-inline-start: 4px;
  opacity: 1;
  outline: none;
  padding-bottom: 2px;
  padding-inline-start: 3px;
  padding-inline-end: 3px;
  padding-top: 2px;
  width: 1.2em;
}

selectmenu:disabled::-internal-selectmenu-button {
  background-color: -internal-light-dark(rgba(239, 239, 239, 0.3), rgba(59, 59, 59, 0.3));
  border-color: -internal-light-dark(rgba(118, 118, 118, 0.3), rgba(195, 195, 195, 0.3));
  color: -internal-light-dark(rgba(16, 16, 16, 0.3), rgba(255, 255, 255, 0.3));
  opacity: 0.7;
}

selectmenu option:disabled {
  background-color: initial;
  color: GrayText;
}

/* remove highlight on disabled options, making them visually un-selectable  */
selectmenu option:disabled:hover {
  background-color: transparent;
}

selectmenu option:checked:disabled {
  background-color: -internal-light-dark(rgb(176, 176, 176), rgba(59, 59, 59, 0.3));
}

selectmenu::-internal-selectmenu-listbox {
  box-shadow: 0px 12.8px 28.8px rgba(0, 0, 0, 0.13), 0px 0px 9.2px rgba(0, 0, 0, 0.11);
  box-sizing: border-box;
  overflow: auto;
  background-color: Field;
  border: 1px solid rgba(0, 0, 0, 0.15);
  border-radius: 0.25em;
  padding: 0.25em 0;
}

/* TODO(github.com/openui/open-ui/issues/645): We need a better way to correctly
 * select author-provided listboxes. This doesn't work if the listbox is
 * provided from an outer tree scope. */
selectmenu [behavior=listbox],
selectmenu::-internal-selectmenu-listbox {
  margin: 0;
  inset: auto;
  min-inline-size: anchor-size(self-inline);
  min-block-size: 1lh;
  anchor-scroll: implicit;
  position-fallback: -internal-selectmenu-listbox-default-fallbacks;
}

@position-fallback -internal-selectmenu-listbox-default-fallbacks {
  /* Below the anchor, left-aligned, no vertical scrolling */
  @try {
    inset-block-start: anchor(self-end);
    inset-inline-start: anchor(self-start);
  }
  /* Below the anchor, right-aligned, no vertical scrolling */
  @try {
    inset-block-start: anchor(self-end);
    inset-inline-end: anchor(self-end);
  }
  /* Over the anchor, left-aligned, no vertical scrolling */
  @try {
    inset-block-end: anchor(self-start);
    inset-inline-start: anchor(self-start);
  }
  /* Over the anchor, right-aligned, no vertical scrolling */
  @try {
    inset-block-end: anchor(self-start);
    inset-inline-end: anchor(self-end);
  }
  /* Below the anchor, left-aligned, fill up the available vertical space */
  @try {
    inset-block-start: anchor(self-end);
    block-size: -webkit-fill-available;
    inset-inline-start: anchor(self-start);
  }
  /* Below the anchor, right-aligned, fill up the available vertical space */
  @try {
    inset-block-start: anchor(self-end);
    block-size: -webkit-fill-available;
    inset-inline-end: anchor(self-end);
  }
  /* Over the anchor, left-aligned, fill up the available vertical space */
  @try {
    inset-block-end: anchor(self-start);
    block-size: -webkit-fill-available;
    inset-inline-start: anchor(self-start);
  }
  /* Over the anchor, right-aligned, fill up the available vertical space */
  @try {
    inset-block-end: anchor(self-start);
    block-size: -webkit-fill-available;
    inset-inline-end: anchor(self-end);
  }
}

